const CryptoJS = require('crypto-js');

    // document.write("<script type='text/javascript' src='./crypto-js-4.0.0/crypto-js.js'></script>");
//加密顺序  AES加密->base64编码
//解密顺序 base64解码->AES加密 用不到base64对中文编码解码，所以暂时可以用浏览器自带的atob和btoa这两个方法
//浏览器自带的性能好，但不支持utf8
//var iasdofiuasoi1 = btoa(encodeURIComponent(message));
//var iasdofiuasoi2 = decodeURIComponent(atob(iasdofiuasoi1))  用encodeURIComponent编码就可以支持中文了

class CryptoFile {
    constructor() {
            // var newscript = document.createElement('script');

            // newscript.setAttribute('src', './crypto-js-4.0.0/crypto-js.js');
            // var head = document.getElementsByTagName('head')[0];
            // head.appendChild(newscript);
            // console.log(123123123123123123)


        //CryptoJS.enc.Utf8.parse() 或 CryptoJS.enc.Latin1.parse()
        // 秘钥
        this.key = CryptoJS.enc.Utf8.parse('XAA4GNADCBiQKBgQ'); // 16位
        this.iv = CryptoJS.enc.Utf8.parse('7vjTH84ppzmuEpH7');
    }
    // 加密
    encrypt(word) {
        if((!word)||(word.length==0)){
            return '';
        }

        let words = CryptoJS.enc.Utf8.parse(word);//words 是一个object 里面有按照utf8编码的数组
         console.log((words))
        // console.log(typeof words)
        // console.log(typeof word)
        // //var decodedData = self.btoa(encodeURIComponent(encryptedText));
        let encrypted = CryptoJS.AES.encrypt(words, this.key, {iv: this.iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

        let ttt = encrypted.ciphertext.toString().toUpperCase();

        // console.log(encodeURIComponent(ttt))
        let tt2 = self.btoa((ttt))

        return tt2
         //console.log(self.btoa(tt2))
        // //var encoded = BASE64.urlsafe_encode(encrypted.toString());
        // let encryptedText = encrypted.toString()
        // //var decodedData = self.btoa(encryptedText);

        //  var decodedData = self.btoa(encodeURIComponent(ttt));
        //  console.log(decodedData)
        // return decodedData;//encrypted.toString();//encoded;
    }

    // 解密
    decrypt(word) {
        if((!word)||(word.length==0)){
            return '';
        }
        // let xyz = decodeURIComponent(atob(word));//word;//BASE64.urlsafe_decode(word)
        // console.log(xyz)
        // let decrypt = CryptoJS.AES.decrypt(xyz, this.key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

        // //let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        // let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        // console.log(decryptedStr)
        // return decryptedStr.toString();
        let xyz = decodeURIComponent(atob(word));
        let encryptedHexStr = CryptoJS.enc.Hex.parse(xyz);
        let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
        let decrypt = CryptoJS.AES.decrypt(srcs, this.key, { iv: this.iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
        let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        return decryptedStr.toString();
    }
}
export default CryptoFile;
